package com.service;

import com.mapper.BrandMapper;
import com.mapper.usermapper;
import com.pojo.Brand;
import com.pojo.user;
import com.util.factory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserService {
    //        调用brandmapper的方法
    SqlSessionFactory sqlSessionFactory = factory.getSqlSessionFactory();

    /**
     * 登录方法
     * @return
     */

    public user login(String name,String password){
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获取brandmapper
        usermapper mapper = sqlSession.getMapper(usermapper.class);
//          调用查询的方法
        user u = mapper.select(name, password);

        return u;
    }

    /**
     * 注册
     * @param name
     * @return
     */
    public boolean add(String name,String mm){
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获取brandmapper
        usermapper mapper = sqlSession.getMapper(usermapper.class);
//          调用查询的方法,查询用户名是否存在
        user u = mapper.select2(name);


        user u2 = new user(name,mm);
//不存在则添加
        if(u==null)
        {
            mapper.add2(u2);
            sqlSession.commit();
            sqlSession.close();
        }
//  u为0则可以注册，返回true
        return u==null;
    }


}
